//
//  CourseEvaluateView.m
//  优学荟管理系统
//
//  Created by 星道 on 17/7/13.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "CourseEvaluateView.h"

@implementation CourseEvaluateView
{
    IBOutletCollection(UIButton) NSArray *_Buttons;
    IBOutlet UILabel *_StateStr;
    IBOutlet UILabel *_Title;
    void(^_SelectItem)(NSString *, NSInteger);
}

//  类方法实例
+ (instancetype)ConfigViewWithTitle:(NSString *)Title Y:(NSInteger)Y SelectItem:(void (^)(NSString *, NSInteger))SelectItem
{
    return [[self alloc] initWithTitle:Title Y:Y SelectItem:SelectItem];
}

// 实例化
- (instancetype)initWithTitle:(NSString *)Title Y:(NSInteger)Y SelectItem:(void (^)(NSString *, NSInteger))SelectItem
{
    self = [[[NSBundle mainBundle] loadNibNamed:@"CourseEvaluateView" owner:nil options:nil] lastObject];
    if (self)
    {
        _Title.text = Title;
        _SelectItem = SelectItem;
    }
    return self;
}




- (IBAction)ClickButton:(UIButton *)sender
{
    [_Buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        obj.selected = idx <= [self ButtonTag:sender.tag] ? YES:NO;
    }];
    
    _StateStr.text = [self GetStateStr:sender.tag - 1100];
    
    _SelectItem ? _SelectItem(_Title.text, sender.tag - 1100) : nil;
}


/** 根据Index得到字符状态 */
- (NSString *)GetStateStr:(NSInteger)Index {
    
    if (Index == 4) {
        
        return @"差";
    } else if (Index == 3) {
        
        return @"中";
    } else if (Index == 2) {
        
        return @"良";
    } else if (Index == 1){
        
        return @"优";
    } else {
        
        return @"";
    }
}

- (NSInteger)ButtonTag:(NSInteger)Tag {
    
    if (Tag == 1101) {
        
        return 3;
    }
    
    if (Tag == 1102) {
        
        return 2;
    }
    
    if (Tag == 1103) {
        
        return 1;
    }
    
    if (Tag == 1104) {
        
        return 0;
    }
    
    return 0;
}

- (void)setSouce:(NSString *)Souce {
    
    _Souce = Souce;
    NSInteger Index = 1100 + [Souce integerValue];
    if (Index != 1100) {
    [self ClickButton:[self viewWithTag:Index]];
    }
}
@end
